//
//  XFDLogInPassWordView.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/3.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDLogInPassWordView.h"
#import "XFDLogInInputView.h"
#import <XFDCommon/HBCommon.h>
#import <Masonry/Masonry.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import "XFDCommon/XFDUserManage.h"
@interface XFDLogInPassWordView()

@property (nonatomic, strong)XFDLogInInputView *phoneView;
@property (nonatomic, strong)XFDLogInInputView *passWordView;
@property (nonatomic, strong)UIButton *sureButton;
@property (nonatomic, strong)UIButton *forgetButton;


@end

@implementation XFDLogInPassWordView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
        [self  handlingAction];
    }
    return self;
}
- (void)handlingAction{
    
    WEAKSELF
    self.phoneView.getTextFromeInputView = ^(NSString *str) {
       STRONGSELF
        [strongSelf handleSureButton];
    };
    self.passWordView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        [strongSelf handleSureButton];
    };
    
}

- (void)handleSureButton{
    
    if (self.phoneView.inputText.length >0 && self.passWordView.inputText.length >0){
        self.sureButton.backgroundColor = GlobalBuleColor;
        self.sureButton.userInteractionEnabled = YES;
    }else{
        self.sureButton.backgroundColor = GlobalGrayOneColor;
        self.sureButton.userInteractionEnabled = NO;
    }
    
}
- (void)createSubViews{
    [self addSubview:self.phoneView];
    [self addSubview:self.passWordView];
    [self addSubview:self.sureButton];
    [self addSubview:self.forgetButton];
    NSString *str = [XFDUserManage accountPhone];
    if (str.length >0) {
        self.phoneView.defultText = str;
    }
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).priorityHigh();
        make.right.equalTo(self.mas_right).priorityHigh();
        make.left.equalTo(self.mas_left).priorityHigh();
        make.height.mas_offset(50).priorityHigh();
    }];
    [self.passWordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneView. mas_bottom).priorityHigh();
        make.right.equalTo(self.mas_right).priorityHigh();
        make.left.equalTo(self.mas_left).priorityHigh();
        make.height.mas_offset(50).priorityHigh();
    }];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passWordView. mas_bottom).with.offset(50).priorityHigh();
        make.right.equalTo(self.mas_right).with.offset(-40).priorityHigh();
        make.left.equalTo(self.mas_left).with.offset(40).priorityHigh();
        make.height.mas_offset(40).priorityHigh();
    }];
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sureButton. mas_bottom).priorityHigh();
        make.right.equalTo(self.mas_right).with.offset(-40).priorityHigh();
        make.width.mas_offset(60).priorityHigh();
        make.height.mas_offset(50).priorityHigh();
    }];
    
}



- (XFDLogInInputView *)phoneView{
    if (!_phoneView) {
        _phoneView = [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
        _phoneView.placeholder = @"请输入用户名或手机号";
        _phoneView.isClear = YES;
    }
    return _phoneView;
}

- (XFDLogInInputView *)passWordView{
    if (!_passWordView) {
        _passWordView = [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
        _passWordView.placeholder = @"请输入密码";
        _passWordView.isCiphertext = YES;
        _passWordView.isClear = YES;
    }
    return _passWordView;
}

- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"登录" forState:UIControlStateNormal];
        _sureButton.backgroundColor = GlobalGrayOneColor;
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sureButton.layer.cornerRadius = 5;
        _sureButton.layer.masksToBounds = YES;
        WEAKSELF
        [_sureButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            if (strongSelf.logInBlock) {
                strongSelf.logInBlock(strongSelf.phoneView.inputText, strongSelf.passWordView.inputText);
            }
        } forControlEvents:UIControlEventTouchUpInside];
        _sureButton.userInteractionEnabled = NO;
    }
    return _sureButton;
}

- (UIButton *)forgetButton{
    if (!_forgetButton) {
        _forgetButton =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgetButton setTitleColor:GlobalBlackOneColor forState:UIControlStateNormal];
        _forgetButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _forgetButton.titleLabel.textAlignment = NSTextAlignmentRight;
        WEAKSELF
        [_forgetButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            if (strongSelf.forgetPassWordBlock) {
                strongSelf.forgetPassWordBlock();
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetButton;
}

@end
